package com.jzy.gulimall.seckill.controller;

import com.jzy.common.utils.R;
import com.jzy.gulimall.seckill.service.SeckillService;
import com.jzy.gulimall.seckill.to.SeckillSkuRedisTo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author jzy
 * @date 2020/9/24 8:47
 */
@Controller
public class SeckillController {

    @Resource
    private SeckillService seckillService;

    /**
     * 查询当前时间的秒杀商品
     */
    @GetMapping("/currentSeckillSkus")
    @ResponseBody
    public R getCurrentSeckillSkus() {
        List<SeckillSkuRedisTo> vos = seckillService.getCurrentSeckillSkus();
        return R.ok().setData(vos);
    }

    /**
     * 查询当前商品的秒杀信息
     */
    @GetMapping("/skuSeckillInfo/{skuId}")
    @ResponseBody
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId) {
        SeckillSkuRedisTo to = seckillService.getSkuSeckillInfo(skuId);
        return R.ok().setData(to);
    }


    /**
     * 处理秒杀请求
     */
    @GetMapping("/kill")
    public String seckill(@RequestParam("killId") String killId,
                               @RequestParam("key") String key,
                               @RequestParam("num") Integer num,
                                Model model) {
        String orderSn = seckillService.kill(killId, key, num);
        if (StringUtils.isNotBlank(orderSn)) {
            model.addAttribute("orderSn", orderSn);
        }
        return "success";
    }
}
